package work.base.designpattern.factory;

// 抽象产品：按钮
interface Button {
    void click();
}

// 具体产品：Windows按钮
class WindowsButton implements Button {
    @Override
    public void click() {
        System.out.println("Windows Button clicked");
    }
}

// 具体产品：Mac按钮
class MacButton implements Button {
    @Override
    public void click() {
        System.out.println("Mac Button clicked");
    }
}

// 抽象产品：文本框
interface TextBox {
    void input();
}

// 具体产品：Windows文本框
class WindowsTextBox implements TextBox {
    @Override
    public void input() {
        System.out.println("Windows TextBox input");
    }
}

// 具体产品：Mac文本框
class MacTextBox implements TextBox {
    @Override
    public void input() {
        System.out.println("Mac TextBox input");
    }
}

// 抽象工厂
interface GUIFactory {
    Button createButton();

    TextBox createTextBox();
}

// 具体工厂：Windows工厂
class WindowsFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new WindowsButton();
    }

    @Override
    public TextBox createTextBox() {
        return new WindowsTextBox();
    }
}

// 具体工厂：Mac工厂
class MacFactory implements GUIFactory {
    @Override
    public Button createButton() {
        return new MacButton();
    }

    @Override
    public TextBox createTextBox() {
        return new MacTextBox();
    }
}


/**
 * 抽象工厂模式
 */
// 客户端代码
public class AbstractFactoryMain {
    public static void main(String[] args) {
        // 使用Windows工厂
        GUIFactory windowsFactory = new WindowsFactory();
        createUI(windowsFactory);

        // 使用Mac工厂
        GUIFactory macFactory = new MacFactory();
        createUI(macFactory);
    }

    public static void createUI(GUIFactory factory) {
        Button button = factory.createButton();
        TextBox textBox = factory.createTextBox();

        button.click();
        textBox.input();
    }
}
